package cn.iocoder.yudao.module.train.controller.admin.teaching;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.teaching.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.teaching.TeachingDO;
import cn.iocoder.yudao.module.train.service.teaching.TeachingService;

@Tag(name = "管理后台 - 授课管理")
@RestController
@RequestMapping("/train/teaching")
@Validated
public class TeachingController {

    @Resource
    private TeachingService teachingService;

    @PostMapping("/create")
    @Operation(summary = "创建授课管理")
    @PreAuthorize("@ss.hasPermission('train:teaching:create')")
    public CommonResult<Long> createTeaching(@Valid @RequestBody TeachingSaveReqVO createReqVO) {
        return success(teachingService.createTeaching(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新授课管理")
    @PreAuthorize("@ss.hasPermission('train:teaching:update')")
    public CommonResult<Boolean> updateTeaching(@Valid @RequestBody TeachingSaveReqVO updateReqVO) {
        teachingService.updateTeaching(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除授课管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:teaching:delete')")
    public CommonResult<Boolean> deleteTeaching(@RequestParam("id") Long id) {
        teachingService.deleteTeaching(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除授课管理")
                @PreAuthorize("@ss.hasPermission('train:teaching:delete')")
    public CommonResult<Boolean> deleteTeachingList(@RequestParam("ids") List<Long> ids) {
        teachingService.deleteTeachingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得授课管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:teaching:query')")
    public CommonResult<TeachingRespVO> getTeaching(@RequestParam("id") Long id) {
        TeachingDO teaching = teachingService.getTeaching(id);
        return success(BeanUtils.toBean(teaching, TeachingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得授课管理分页")
    @PreAuthorize("@ss.hasPermission('train:teaching:query')")
    public CommonResult<PageResult<TeachingRespVO>> getTeachingPage(@Valid TeachingPageReqVO pageReqVO) {
        PageResult<TeachingDO> pageResult = teachingService.getTeachingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeachingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出授课管理 Excel")
    @PreAuthorize("@ss.hasPermission('train:teaching:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeachingExcel(@Valid TeachingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TeachingDO> list = teachingService.getTeachingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "授课管理.xls", "数据", TeachingRespVO.class,
                        BeanUtils.toBean(list, TeachingRespVO.class));
    }

}